if [[ `cat /etc/*-release|grep -E 'ubuntu[^ ]*|debian[^ ]*'` ]];then
[ -x "$(command -v wget)" ] || apt install wget -y
[ -x "$(command -v netstat)" ] || apt install net-tools -y
elif [[ `cat /etc/*-release|grep -E 'centos[^ ]*|fedora[^ ]*'` ]];then
[ -x "$(command -v wget)" ] || yum install wget -y
[ -x "$(command -v netstat)" ] || yum install net-tools -y
fi

IP=`wget -qO- -t1 -T2 api.ip.sb/ip`

echo "检测 Docker......"
if [ -x "$(command -v docker)" ]; then
echo "检测到 Docker 已安装!"
else
echo "安装 docker 环境..."
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
echo "安装 docker 环境...安装完成!"
systemctl enable docker
systemctl start docker
fi

echo -n -e "本机ip: \e[31m$IP\e[0m\n"

stty erase ^? && read -p "输入青龙容器名:" QLNAME
check_name(){
echo "正在检测青龙容器名:$1"
docker ps | grep "\b$1\b"
}
if [ "$port" != "2" ]; then
while check_name $QLNAME; do    
echo -n -e "\e[31m青龙容器名:$QLNAME 已创建，请重新输入青龙容器名：\e[0m"
read QLNAME
done
echo -e "\e[34m恭喜，青龙容器名:$QLNAME 已创建成功\e[0m"
fi

#docker stop $QLNAME
#dicker rm $QLNAME

check_port(){
echo "正在检测端口:$1"
netstat -tlpn | grep "\b$1\b"
}

stty erase ^? && read -p "青龙端口:" QLPORT
if [ "$port" != "2" ]; then
while check_port $QLPORT; do    
echo -n -e "\e[31m端口:$QLPORT 被占用，请重新输入青龙面板端口：\e[0m"
read QLPORT
done
echo -e "\e[34m恭喜，端口:$QLPORT 可用\e[0m"
fi

stty erase ^? && read -p "ninja端口:" NINJAPORT
if [ "$port" != "2" ]; then
while check_port $NINJAPORT; do    
echo -n -e "\e[31m端口:$NINJAPORT 被占用，请重新输入青龙面板端口：\e[0m\n"
read NINJAPORT
done
echo -e "\e[34m恭喜，端口:$NINIAPORT 可用\e[0m\n"
fi

docker run -dit \
-v $PWD/ql/config:/ql/config \
-v $PWD/ql/log:/ql/log \
-v $PWD/ql/db:/ql/db \
-v $PWD/ql/repo:/ql/repo \
-v $PWD/ql/raw:/ql/raw \
-v $PWD/ql/scripts:/ql/scripts \
-v $PWD/ql/jbot:/ql/jbot \
-v $PWD/ql/ninja:/ql/ninja \
-p $QLPORT:5700 \
-p $NINJAPORT:5701 \
--name $QLNAME \
--hostname qinglong \
--restart unless-stopped \
shufflewzc/qinglong:latest

docker update --restart=always $QLNAME

docker exec -it $QLNAME bash -c "cd /ql;ps -ef|grep ninja|grep -v grep|awk '{print $1}'|xargs kill -9;rm -rf /ql/ninja;git clone https://ghproxy.com/https://github.com/Hnsyx/Waikiki_ninja /ql/ninja;cd /ql/ninja/backend;pnpm install;cp .env.example .env;cp sendNotify.js /ql/scripts/sendNotify.js;sed -i \"s/ALLOW_NUM=40/ALLOW_NUM=100/\" /ql/ninja/backend/.env;pm2 start"
 
docker exec -it $QLNAME bash -c "sed -i \"s/ALLOW_NUM=40/ALLOW_NUM=100/\" /ql/ninja/backend/.env && cd /ql/ninja/backend && pm2 start"

docker exec -it $QLNAME bash -c "echo 'cd /ql/ninja/backend;pm2 start' >>/ql/config/extra.sh;touch /ql/sample/requirements.txt;ql update;ql check"

docker exec -it $QLNAME bash -c "ql repo https://ghproxy.com/https://github.com/shufflewzc/faker2.git"
# "jd_|jx_|gua_|jddj_|getJDCookie" "activity|backUp" "^jd[^_]|USER|ZooFaker_Necklace.js|JDJRValidator_Pure|sign_graphics_validate"

echo -n -e "执行 \e[31mdocker exec -it $QLNAME bash -c \"cat /ql/config/auth.json\"\e[0m 查看用户名和密码\n"""

echo -n -e "青龙面板%浏览器输入: \e[31mhttp://$IP:$QLPORT\e[0m\n"
echo -n -e "ninja面板%浏览器输入: \e[31mhttp://$IP:$NINJAPORT\e[0m\n"
